﻿using FlyHorse.LIMS.IBLL;
using FlyHorse.LIMS.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FlyHorse.LIMS.BLL
{
    public partial class GoodBll : BaseBll<Good>, IGoodBll
    {
        public List<Good> GetRawGoodList()
        {
            var models = from m in CurrentDBSession.GoodDal.LoadEntitiesAsNoTracking(m => m.GoodsTypeID == "1") select m;
            return models.ToList();
        }
        public List<Good> GetProductGoodList()
        {
            var models = from m in CurrentDBSession.GoodDal.LoadEntitiesAsNoTracking(m => m.GoodsTypeID == "2") select m;
            return models.ToList();
        }




        /// <summary>
        /// 删除样品名称和标准
        /// </summary>
        /// <param name="Good"></param>
        /// <returns></returns>
        public bool DeleteGoodAndStandard(Good Good)
        {
            GoodsType GoodType = this.CurrentDBSession.GoodsTypeDal.LoadEntitiesAsNoTracking(m => m.GoodsTypeID == Good.GoodsTypeID).First();
            this.CurrentDBSession.Db.Set<Good>().Remove(Good);
            if (GoodType.GoodsTypeName == "原材料")
            {
                List<StandardRaw> rawStandardList = this.CurrentDBSession.StandardRawDal.LoadEntitiesAsNoTracking(m => m.GoodsID == Good.GoodsID).ToList();
                foreach (var rawStandard in rawStandardList)
                {
                    var model = this.CurrentDBSession.StandardRawDal.LoadEntities(m => m.StandardRawID == rawStandard.StandardRawID).First();
                    this.CurrentDBSession.Db.Set<StandardRaw>().Remove(model);
                }
            }
            else if (GoodType.GoodsTypeName == "产品")
            {
                List<StandardSemi> standardSemiList = this.CurrentDBSession.StandardSemiDal.LoadEntitiesAsNoTracking(m => m.GoodsID == Good.GoodsID).ToList();
                List<StandardCustomer> standardCustomerList = this.CurrentDBSession.StandardCustomerDal.LoadEntitiesAsNoTracking(m => m.GoodsID == Good.GoodsID).ToList();
                foreach (var standardSemi in standardSemiList)
                {
                    var model = this.CurrentDBSession.StandardSemiDal.LoadEntities(m => m.StandardSemiID == standardSemi.StandardSemiID).First();
                    this.CurrentDBSession.Db.Set<StandardSemi>().Remove(model);
                }
                foreach (var standardCustomer in standardCustomerList)
                {
                    var model = this.CurrentDBSession.StandardCustomerDal.LoadEntities(m => m.StandardCustomerID == standardCustomer.StandardCustomerID).First();
                    this.CurrentDBSession.Db.Set<StandardCustomer>().Remove(model);
                }
            }
            else
            {
                return false;
            }
            int count = this.CurrentDBSession.Db.SaveChanges();
            if (count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}
